package
{	
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.getTimer;
	import com.gabriellaet.subtitle.SRT;
	import flash.text.TextField;
	import com.gabriellaet.subtitle.Subtitle;
	
	[SWF(width="550", height="400", backgroundColor="#FFFFFF", frameRate="60")]
	public class SubtitleTester extends Sprite
	{
		public var loader:URLLoader;
		public var output:TextField;
		public var timer:TextField;
		
		public function SubtitleTester()
		{
			loader = new URLLoader();
			loader.addEventListener(IOErrorEvent.IO_ERROR, _onLoadError, false, 0, true);
			loader.addEventListener(Event.COMPLETE, _onLoadComplete, false, 0, true);
			loader.load(new URLRequest("../subtitles/pulp_fiction.srt"));
			
			output = new TextField();
			output.width = 550;
			output.height = 60;
			output.border = true;
			output.y = stage.stageHeight - output.height;
			addChild(output);
			
			timer = new TextField();
			timer.width = 100;
			timer.height = 30;
			timer.x = stage.stageWidth - timer.width;
			addChild(timer);
		}
		
		public function _onLoadComplete(e:Event):void
		{
			var srt:SRT = new SRT();
			srt.parse(loader.data);
			addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				var t:int = getTimer();
				var s:Subtitle = srt.get(t);
				output.text = s == null ? "" : s.text;
				timer.text = String(t);
			});
		}
		
		public function _onLoadError(e:IOErrorEvent):void
		{
		}
		
	}
}